<%@tag import="org.apache.log4j.Logger"%>
<%@tag import="java.sql.Connection"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhTask"%>
<%@tag import="by.epam.communalservices.entity.JkhTask"%>
<%@ attribute name="id" required="true"%>
<%@ attribute name="idObject" required="true"%>
<%@ attribute name="idTeam" required="true"%>
<%@ attribute name="jobDescription" required="true"%>
<%@ attribute name="jobRequest" required="true"%>
<%@ attribute name="performTime" required="true"%>
<%@ attribute name="status" required="true"%>
<%@ attribute name="delete" required="true"%>
<%@ attribute name="submit" required="true"%>
<%
    request.setAttribute("message", "");

    Connection con = (Connection) request.getSession().getAttribute("currentConnection");
    Logger taskEditLogger = Logger.getLogger("taskEditLogger");
    JkhTask jkhTask = null;
    JdbcDaoJkhTask daoTask = new JdbcDaoJkhTask(con);

    try {
        if (getJspContext().getAttribute("id") != ""
                && Long.parseLong(getJspContext().getAttribute("id").toString()) > 0) {
            jkhTask = daoTask.read(Long.parseLong(getJspContext().getAttribute("id").toString()));
            if (getJspContext().getAttribute("submit") != "") {
                if (getJspContext().getAttribute("idObject").toString()!="") {
                    jkhTask.setIdObject(Long.parseLong(getJspContext().getAttribute("idObject").toString()));
                }
                if (getJspContext().getAttribute("idTeam").toString() != "") {
                    jkhTask.setIdTeam(Long.parseLong(getJspContext().getAttribute("idTeam").toString()));
                }

                if (getJspContext().getAttribute("jobDescription").toString()!="") {
	                jkhTask.setJobDescription(getJspContext().getAttribute("jobDescription").toString()
                        .replaceAll("\n", "<br>"));
                }
                if (getJspContext().getAttribute("performTime").toString()!="") {
                    jkhTask.setPerformTime(Double.parseDouble(getJspContext().getAttribute("performTime").toString()));
                }
                if (getJspContext().getAttribute("status").toString()!="") {
                	jkhTask.setStatus(getJspContext().getAttribute("status").toString());
                }

                if (getJspContext().getAttribute("jobRequest").toString()!="") {
	                jkhTask.setRequest(getJspContext().getAttribute("jobRequest").toString()
                        .replaceAll("\n", "<br>"));
                }
                
                daoTask.update(jkhTask.getId(), jkhTask);
                request.setAttribute("message", "Task updated");
            }
        } else if (getJspContext().getAttribute("delete") != ""
                && Long.parseLong(getJspContext().getAttribute("delete").toString()) > 0) {
            daoTask.delete(Long.parseLong(getJspContext().getAttribute("delete").toString()));
            request.setAttribute("message", "Task deleted");
        } else {
            jkhTask = new JkhTask();
            if (getJspContext().getAttribute("idObject").toString()!="") {
                jkhTask.setIdObject(Long.parseLong(getJspContext().getAttribute("idObject").toString()));
            	jkhTask.setStatus("0");
            }
            if (getJspContext().getAttribute("submit") != "") {
                if (getJspContext().getAttribute("idTeam").toString() != "") {
                    jkhTask.setIdTeam(Long.parseLong(getJspContext().getAttribute("idTeam").toString()));
                }

                if (getJspContext().getAttribute("jobDescription").toString()!="") {
	                jkhTask.setJobDescription(getJspContext().getAttribute("jobDescription").toString()
                        .replaceAll("\n", "<br>"));
                }
                if (getJspContext().getAttribute("performTime").toString()!="") {
                    jkhTask.setPerformTime(Double.parseDouble(getJspContext().getAttribute("performTime").toString()));
                }
                if (getJspContext().getAttribute("status").toString()!="") {
                	jkhTask.setStatus(getJspContext().getAttribute("status").toString());
                }

                if (getJspContext().getAttribute("jobRequest").toString()!="") {
	                jkhTask.setRequest(getJspContext().getAttribute("jobRequest").toString()
                        .replaceAll("\n", "<br>"));
                }
                
                jkhTask.setId(daoTask.create(jkhTask));
                request.setAttribute("message", "Task Created");
            }
        }

    } catch (Exception ex) {
        request.setAttribute("message", "ERROR");
        taskEditLogger.error("Error editTask tag", ex);
    }
    if (jkhTask == null) {
        request.setAttribute("form", false);
        if (getJspContext().getAttribute("delete") == "") {
            request.setAttribute("message", "Task Not Found");
        }
    } else {
        request.setAttribute("form", true);
        request.setAttribute("task", jkhTask);
    }
%>